let bleno = require('bleno');

let characteristics = require('./characteristic');

let adxl = require('./adxl');

let BlenoPrimaryService = bleno.PrimaryService;

let app = require('express')();
let http = require('http').Server(app);

app.get('/', (req, res) => {
    res.render('index.ejs');
});

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');

http.listen(3000, () => {
    console.log('http listen on 3000');
});

bleno.on('stateChange', function (state) {
    console.log('on -> stateChange: ' + state);

    if (state === 'poweredOn') {
        bleno.startAdvertising('james zwift', ['1801', '1800', '1814']);
    } else {
        bleno.stopAdvertising();
    }
});

bleno.on('advertisingStart', function (error) {
    console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));

    if (!error) {
        bleno.setServices([
            new BlenoPrimaryService(
                {
                    uuid: '1801',
                    characteristics: [
                        new characteristics.AttributeCharacteristic()
                    ]
                }),
            new BlenoPrimaryService(
                {
                    uuid: '1800',
                    characteristics: [
                        new characteristics.DeviceCharacteristic(),
                        new characteristics.AppearanceCharacteristic(),
                        new characteristics.CentralAddressResolutionCharacteristic()
                    ]
                }),
            new BlenoPrimaryService(
                {
                    uuid: '1814',
                    characteristics: [
                        new characteristics.RSCMeasurementCharacteristic(),
                        new characteristics.RSCFeatureCharacteristic()
                    ]
                }),
            new BlenoPrimaryService(
                {
                    uuid: 'FFD0',
                    characteristics: [
                        new characteristics.ZZDPControllerCharacteristic(),
                        new characteristics.ZZDPDataCharacteristic()
                    ]
                })
        ]);

    }
});
